import requests
import execjs
import json
import ddddocr

ocr = ddddocr.DdddOcr(det=False, ocr=False, show_ad=False)

with open('./parse/4.超星v8.js', 'r') as f:
    js_xtr = f.read()

js_code = execjs.compile(js_xtr, cwd='./parse')
params = js_code.call('get_img_request')

res = requests.get('https://captcha.chaoxing.com/captcha/get/verification/image', params=params, headers={
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36'
})
data = json.loads(res.text[20:-1])
print(data)

token = data['token']
bg_url = data['imageVerificationVo']['shadeImage']
target_url = data['imageVerificationVo']['cutoutImage']
bg_res = requests.get(bg_url, headers={
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36'
})
target_res = requests.get(target_url, headers={
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36'
})
bg_bytes = bg_res.content
target_bytes = target_res.content
with open('./static/yzm/v8_bg.png', 'wb') as f:
    f.write(bg_bytes)
with open('./static/yzm/v8_target.png', 'wb') as f:
    f.write(target_bytes)

r = ocr.slide_match(target_bytes, bg_bytes)
print(r['target'][0])

params = js_code.call('get_img_result', token, r['target'][0])

res2 = requests.get('https://captcha.chaoxing.com/captcha/check/verification/result', params=params, headers={
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36',
    'referer': 'https://v8.chaoxing.com/'
})
print(res2)
